cmake_minimum_required(VERSION 2.8)

if(WIN32)
  project(Libevent)
  set(Libevent_SRC
    event.c
    buffer.c
    bufferevent.c
    bufferevent_sock.c
    bufferevent_pair.c
    listener.c
    evmap.c
    log.c
    evutil.c
    strlcpy.c
    signal.c
    bufferevent_filter.c
    evthread.c
    bufferevent_ratelim.c
    evutil_rand.c
    win32select.c
    evthread_win32.c
    buffer_iocp.c
    event_iocp.c
    bufferevent_async.c
    event_tagging.c
    http.c
    evdns.c
    evrpc.c
  )
  include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/WIN32-Code
    ${PROJECT_SOURCE_DIR}/compat
  )
  add_library(Libevent STATIC ${Libevent_SRC})
elseif(UNIX)
  include(ExternalProject)
  if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
    # ACE Won't build on FreeBSD 10 with the exceptions test enabled so we're just turning that test off
    set(DISABLE_EXCEPTIONS "--enable-exceptions=no")
  else()
    set(DISABLE_EXCEPTIONS "")
  endif()

  ExternalProject_Add(Libevent_Project
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}
    INSTALL_DIR ${CMAKE_INSTALL_PREFIX}
    DOWNLOAD_COMMAND ""
    CONFIGURE_COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/configure --prefix=${CMAKE_INSTALL_PREFIX} ${DISABLE_EXCEPTIONS}
  )
else()
  message(FATAL_ERROR
    "This script doesn't support your system configuration to compile/install Libevent."
  )
endif()
